﻿using Admin.Repository;
using Admin.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Admin.Repository
{
    public class BaseRepository<Entity> : IBaseRepository<Entity> 
        where Entity : class
    {
        //受保护的  在派生类中访问基类的成员
        protected MS_Context db;

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Entity Create(Entity entity)
        {
            //db.Add(entity);
            db.Set<Entity>().Add(entity);
            db.SaveChanges();
            return entity;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Entity Update(Entity entity)
        {
            //db.Update(entity);
            db.Set<Entity>().Update(entity);
            db.SaveChanges();
            return entity;
        }

        /// <summary>
        /// 查询数据 带条件直接查询
        /// </summary>
        /// <returns></returns>
        public IQueryable<Entity> Queryable()
        {
            return db.Set<Entity>();
        }

        /// <summary>
        /// 查询数据 查询全部
        /// 把所有数据查出来，然后再找到自己需要的信息
        /// </summary>
        /// <returns></returns>
        public List<Entity> QueryToList()
        {
            return db.Set<Entity>().ToList();
        }

        /// <summary>
        /// 根据id查询一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Entity QueryId(int id)
        {
            return db.Set<Entity>().Find(id);
        }

        /// <summary>
        /// 查询全部数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public List<Entity> QueryList(Expression<Func<Entity, bool>> predicate)
        {
            return db.Set<Entity>().Where(predicate).ToList();
        }

        /// <summary>
        /// 删除 单删 通过主键
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            var list = db.Set<Entity>().Find(id);
            db.Set<Entity>().Remove(list);
            return db.SaveChanges();
        }
        /// <summary>
        /// 删除 单删 通过实体
        /// </summary>
        /// <returns></returns>
        public int Delete(Entity entity)
        {
            //db.Set<Entity>().Remove(entity);
            db.Entry<Entity>(entity).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
            return db.SaveChanges();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public int Delete(Expression<Func<Entity, bool>> predicate)
        {
            var list = db.Set<Entity>().Where(predicate).ToList();
            db.Set<Entity>().RemoveRange(list);
            return db.SaveChanges();
        }

        /// <summary>
        /// 删除 批删 通过主键
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int Delete(int[] ids)
        {
            foreach (var item in ids)
            {
                var list = db.Set<Entity>().Find(item);
                db.Set<Entity>().Remove(list);
            }
            return db.SaveChanges();
        }
        /// <summary>
        /// 删除 批删 通过实体
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int Delete(List<Entity> entity)
        {
            db.Set<Entity>().RemoveRange(entity);
            return db.SaveChanges();
        }
    }
}
